package com.lee.demo1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.mylib.www.MD5Utils;

import com.lee.demo1.business.SysUserBusiness;
import com.lee.demo1.domain.SysUser;

@Controller
@RequestMapping("sysuser")
public class SysUserController {
	
	private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	@Resource
	private SysUserBusiness sysUserBusiness;
	
	
	/**
	 * 进入用户更新页面
	 * @return
	 */
	@RequestMapping(params = "method=update")
	public String update(){
		return "admin/sysuser/form";
	}
	
	/**
	 * 执行更新用户操作
	 * @param redirectAttributes
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=save")
	public String save(RedirectAttributes redirectAttributes, 
			HttpServletRequest request, Model model){
		
		SysUser sessionSysUser = (SysUser)request.getSession().getAttribute("sessionSysUser");
		
		SysUser sysUser = new SysUser();
		sysUser.setName(request.getParameter("name"));
		sysUser.setId(sessionSysUser.getId());
		String password = MD5Utils.getInstance().encrypt(request.getParameter("password"));
		String sessionSysUserPassword = sessionSysUser.getPassword();
		String password1 = request.getParameter("password1");
		if(StringUtils.equals(password, sessionSysUserPassword)){
			sysUser.setPassword(MD5Utils.getInstance().encrypt(password1));
			sysUserBusiness.updateSysUser(sysUser);
			model.addAttribute("message", 2);//修改成功
		}else{
			model.addAttribute("message", 1);//输入的旧密码不正确
		}
		return "admin/sysuser/form";
	}
	
	
}
